using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DAL;
using BLL;
using DTO;

namespace WellmeadowsHospital
{
    public partial class frmAddAppointment : DevExpress.XtraEditors.XtraUserControl
    {
        StaffBLL staffBLL = new StaffBLL();
        PatientBLL patientBLL = new PatientBLL();
        NextOfKinBLL nokBLL = new NextOfKinBLL();
        LocalDoctorBLL localdoctorBLL = new LocalDoctorBLL();
        AppointmentBLL appointmentBLL = new AppointmentBLL();
        ListWaitBLL listwaitBLL = new ListWaitBLL();
        OutPatientsBLL outpatientsBLL = new OutPatientsBLL();
        string appointmentid;
        public frmAddAppointment()
        {
            InitializeComponent();
            gcPaInAddAppoint.DataSource = patientBLL.SelectNew();
            gcApointListInAddAppoint.DataSource = appointmentBLL.SelectDesc();
            gcStaffInAddAppoint.DataSource = staffBLL.SelectAll();
            ButtonEditAppoitH.Visible = false;
        }

        private void txtSearchPaInAddAppoint_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!txtSearchPaInAddAppoint.Equals(""))
            {
                gcPaInAddAppoint.DataSource = patientBLL.Search(txtSearchPaInAddAppoint.Text);
            }
            else
            {
                gcPaInAddAppoint.DataSource = patientBLL.SelectDesc();
            }
        }

        private void gridView22_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            AddAppointPaIdH.Text = gridView22.GetRowCellValue(gridView22.GetSelectedRows()[0], gcAddAppointPaID).ToString();
        
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            string key = gridView1.GetRowCellValue(gridView1.GetSelectedRows()[0], gcAppointIdH).ToString();
            var mang = appointmentBLL.Search(key)[0];
            AddAppointPaIdH.Text = mang.PatientID;
            AddAppointStaffIdH.Text = mang.StaffID;
            appointmentid = mang.AppointmentID.ToString();
            ButtonEditAppoitH.Visible = true;
        }

        private void gridView23_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            AddAppointStaffIdH.Text = gridView23.GetRowCellValue(gridView23.GetSelectedRows()[0], gcAddAppointStaffID).ToString();
       
        }

        private void AddAppointAdd_Click(object sender, EventArgs e)
        {
            try
            {
                AppointmentDTO nhanappoint = new AppointmentDTO();
                nhanappoint.Staffid = AddAppointStaffIdH.Text;
                nhanappoint.Patientid = AddAppointPaIdH.Text;
                nhanappoint.Date = AddAppointDateH.DateTime;
                nhanappoint.Time = AddAppointTimeH.Time.ToString();
                nhanappoint.Room = AddAppointRoomH.SelectedItem.ToString();
                nhanappoint.Description = AddAppointDescriptionH.Text;
                nhanappoint.Status = AddAppointStatusH.Text;
                if (appointmentBLL.Insert(nhanappoint).Equals("success"))
                {
                    MessageBox.Show("Add Apointment success");
                    gcApointListInAddAppoint.DataSource = appointmentBLL.SelectDesc();
                }
                else
                {
                    MessageBox.Show(appointmentBLL.Insert(nhanappoint));
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

        private void ButtonEditAppoitH_Click(object sender, EventArgs e)
        {
            try
            {
                AppointmentDTO nhanappoint = new AppointmentDTO();
                nhanappoint.Appointmentid = appointmentid;
                nhanappoint.Staffid = AddAppointStaffIdH.Text;
                nhanappoint.Patientid = AddAppointPaIdH.Text;
                nhanappoint.Date = AddAppointDateH.DateTime;
                nhanappoint.Time = AddAppointTimeH.Time.ToString();
                nhanappoint.Room = AddAppointRoomH.SelectedItem.ToString();
                nhanappoint.Description = AddAppointDescriptionH.Text;
                nhanappoint.Status = AddAppointStatusH.SelectedItem.ToString();
                if (appointmentBLL.Update(nhanappoint).Equals("success"))
                {
                    MessageBox.Show("Update Apointment success");
                    gcApointListInAddAppoint.DataSource = appointmentBLL.SelectDesc();
                }
                else
                {
                    MessageBox.Show(appointmentBLL.Update(nhanappoint));
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }
    }
}
